﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ToWaTimeReportMVC4.Models
{
    public class ToWaService
    {

        ToWaTimeReportMVC4Entities db = new ToWaTimeReportMVC4Entities();

        public User ValidateUser(string username, string password)
        {
            User user = null;
            try
            {
                user = db.Users.Single(u => u.Username == username);
            }
            catch { }
            if (user != null)
            {
                if (user.Password.CompareTo(password) == 0)
                {
                    return user;
                }
            }
            return null;
        }

        public User GetUserById(int id)
        {
            User user = db.Users.Single(u => u.Id == id);

            return user;
        }

        public User GetUserByUsername(string username)
        {
            User user = db.Users.Single(u => u.Username == username);

            return user;
        }

        public Job GetJobById(int id)
        {
            Job job = db.Jobs.Single(j => j.Id == id);

            return job;
        }

        public string GetJobDescriptionByJobId(int id)
        {
            var description = db.Jobs.Single(j => j.Id == id).Description;

            return description.ToString();
        }

        public Project GetProjectById(int id)
        {
            Project project = db.Projects.Single(p => p.Id == id);

            return project;
        }

        public List<Project> GetProjectsByConsultant(User consultant, DateTime from, DateTime to)
        {
            bool CheckDates = false;
            if (from != null && to != null)
            {
                if (from.Millisecond < to.Millisecond)
                {
                    CheckDates = true;
                }
            }
            List<UserToJob> JobIds = db.UserToJobs.Where(r => r.UserId == consultant.Id).ToList();
            List<int> Jobs = new List<int>();
            foreach (UserToJob j in JobIds)
            {
                if (CheckDates)
                {
                    if (j.Job.StartTime.CompareTo(from) >= 0 && j.Job.EndTime.CompareTo(to) <= 0)
                    {
                        Jobs.Add(j.Job.ProjectId);
                    }
                }
                else
                {
                    Jobs.Add(j.Job.ProjectId);
                }
            }
            Jobs = Jobs.Distinct().ToList();
            List<Project> Projects = new List<Project>();
            foreach (int i in Jobs)
            {
                Projects.Add(GetProjectById(i));
            }
            return Projects;
        }

        public void AddJob(DateTime startTime, DateTime endTime, string description, int projectId, int consultantId)
        {
            Job newJob = new Job();
            newJob.StartTime = startTime;
            newJob.EndTime = endTime;
            newJob.Description = description;
            newJob.ProjectId = projectId;
            db.Jobs.Add(newJob);
            db.SaveChanges();

            db.UserToJobs.Add(new UserToJob { JobId = newJob.Id, UserId = consultantId });
            db.SaveChanges();
        }

        public void AddProject(string name, int costumerId, string description)
        {
            Project project = new Project();
            project.Name = name;
            project.UserId = costumerId;
            project.Description = description;
            db.Projects.Add(project);
            db.SaveChanges();
        }

        public void AddUser(string username, string password, string salt, string email, int type, int salary, int hoursWorked)
        {
            //User user = new User();
            //user.Username = username;
            //user.Password = password;
            //if (salt != null && salt.Length > 0)
            //{
            //    user.Salt = salt;
            //}
            //else
            //{
            //    user.Salt = "";
            //}
            //user.Email = email;
            //user.Type = type;
            //user.Salary = salary;
            //user.HoursWorked = hoursWorked;
            db.Users.Add(new User { Username = username, Password = password, Salt = salt, Salary = salary, Email = email, Type = type, HoursWorked = hoursWorked });
            db.SaveChanges();
        }

        public void AddConsultantToJob(User consultant, Job job)
        {
            if (consultant.Type == 2)
            {
                db.UserToJobs.Add(new UserToJob { JobId = job.Id, UserId = consultant.Id });
                db.SaveChanges();
            }
        }

        public string GetTypeOfUserByUsername(string username)
        {
            User user = null;
            try
            {
                user = db.Users.Single(u => u.Username == username);
            }
            catch { }

            if (user != null)
            {
                if (user.Type == 0)
                {
                    return "Admin";
                }
                else if (user.Type == 1)
                {
                    return "Customer";
                }
                else if (user.Type == 2)
                {
                    return "Consultant";
                }
                else
                {
                    return "Not a valid type.";
                }
            }
            else
            {
                return "No user found.";
            }
        }

        public int ConvertTypeToInt(string stringType)
        {
            if (stringType.CompareTo("Admin") == 0)
            {
                return 0;
            }
            else if (stringType.CompareTo("Customer") == 0)
            {
                return 1;
            }
            else if (stringType.CompareTo("Consultant") == 0)
            {
                return 2;
            }
            return -1;
        }

    }
}